GBML manual / Text

Drawing text

Text on a GBA is a row of small sprites, one per character, so every word you draw spends from the same budget as everything else on screen.

Text is made of sprites

A modern computer has a text engine: you hand it a sentence and it paints letters. The GBA has no such thing. It only knows how to draw tiles and sprites. So when you draw the word HELLO, the engine builds five small sprites, one for each letter, and lines them up. From then on they are ordinary sprites. They count against the 128 sprites the hardware can draw in one frame, and against the sprite palette banks, like any other sprite. Budgets lists both.

Three consequences follow, and they explain most of this page:

Drawing it

You give draw_text a position and a string. A string is text in a program: letters and symbols between double quotes, like "OW!".

// Draw event of an enemy that has just been hit
draw_text(x, y - 16, "OW!");

Call it from a Draw event (see Drawing and the draw order). It has to be repeated every frame, because the text is shown only while the call keeps being made. Repeating the same words at the same spot is cheap. The engine reuses last frame's sprites, and only rebuilds them when the string, the position or the alignment changes.

There are two drawing functions, plain and warped (scaled and rotated), and they draw in two places, which the event decides:

In a Draw event (scrolls with the camera)In a Draw GUI event (fixed to the screen)
plaindraw_textdraw_text
scaled and rotateddraw_text_warpeddraw_text_warped

In a Draw event, text is about a place: a damage number above an enemy, a name over a character. In a Draw GUI event it is for a score or a health readout that stays put while the room scrolls, and it always draws in front of the whole world. It is the same call in both: no second name to learn. To fix text to the screen from some other event, use draw_text_anchored.

The warped forms take three more values, xscale, yscale and angle, so a title can spin or a score can pop. Angles are in degrees, 0 is east, and turning a bigger angle turns counterclockwise. A negative scale mirrors the whole string. All the letters share one affine matrix, one of the hardware's 32. A warped call with scale 1 and angle 0 needs no matrix at all. If all 32 are already in use somewhere else, the text is still drawn, just unwarped for that frame.

Text can also lean, the way italic writing does. draw_text_sheared takes the warped call's six values and two more, xshear and yshear, and does the scaling, the turning and the leaning in one call. It scrolls with the camera and has no screen-fixed form.

Several lines

Write \n inside the string where a new line should start, and one call draws several lines:

// Draw GUI event
draw_text(8, 8, "GAME OVER\nPRESS START");

Fonts

A font is an asset in your project, like a sprite. font_set(f) chooses which font every text call uses from then on, until you change it. It is a setting you leave switched, not something you pass to each call. Calling it once in a room's setup is usual.

You do not have to call it. If a project draws text and never picks a font, the game uses a default font: plain letters, numbers and punctuation, each 8 pixels wide and 8 tall.

The default font is a real font in your project. The first time you build a game that draws text, Gbabel copies it into your project as assets/fonts/font_default and lists it with your other fonts. From then on it is yours. Open it in the Paint tab and change it like any font you made yourself: the Font Wizard walks you through its letters one at a time. The game starts with it selected, so text drawn before any font_set call looks the way you drew it.

Two things to know:

A project that never draws text never gets a font_default, and nothing is added to its game.

What a font costs:

Alignment

text_align(align) decides what the x you give means. It takes one of ALIGN_LEFT (the default: x is where the text starts), ALIGN_CENTER (x is the middle of the text) or ALIGN_RIGHT (x is where the text ends). Like font_set, it stays in effect until changed. For warped text the same point is also the center that the turning and scaling happens around.

Here is a readout that stays in the top-right corner of the screen while the room scrolls. ALIGN_RIGHT makes x the place where the text ends, so the word lines up against the right edge:

// Draw GUI event of an object that is always in the room
text_align(ALIGN_RIGHT);
draw_text(236, 4, "SCORE");

Where text sits in front and behind

Text follows the same rules as any sprite. Its layer and order come from the instance drawing it: bg_priority first, then depth. A text call at a higher depth draws behind a sprite at a lower one. Only a draw call in a Draw GUI event is different: it ignores bg_priority and sits on the front-most layer.

What text cannot do

Everything in this category

EntryWhat it is
draw_textplain text, in the world (in a Draw event) or on the screen (in a Draw GUI event)
draw_text_warpedscaled and rotated text, in the world or on the screen the same way
draw_text_shearedscaled, rotated and leaning text, in the world
font_setchoose the font (the default one is font_default, a font in your project)
text_alignchoose the alignment

Turning a number into text, so a score can be drawn, is done with string. Joining pieces of text together is string_join.

See also: Drawing and the draw orderdraw_textfont_settext_alignBudgetsdepth